//
//  PYResult.m
//  Piaoyoung
//
//  Created by LiTengFei on 15/6/2.
//  Copyright (c) 2015年 HangZhou PiaoYoung Co.Ltd. All rights reserved.
//
#import "PYResult.h"

@interface PYResult ()

@property(nonatomic, strong) NSDictionary *dictionary;
@end

@implementation PYResult

-(instancetype)init{
    return [self initWithDictionary:@{}];
}

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}

- (instancetype)initWithDictionary:(NSDictionary *)dictionary {
    self = [super init];
    if (self) {
        [self anasyleData:dictionary];
    }
    return self;
}

+ (instancetype)resultWithDictionary:(NSDictionary *)dictionary {
    PYResult *result = [[PYResult alloc] initWithDictionary:dictionary];
    if(result.codeStyle!=1){
        result.error = [PYError errorWithCode:result.codeStyle message:result.message];
    }
    return result;
}

//- (instancetype)initWithDictionary:(NSDictionary *)dictionary analyseBlock:(PYResult *(^)(NSDictionary *))analyse {
//    PYResult * result = [self initWithDictionary:dictionary];
//    result.analyseBlock = analyse;
//    return result;
//}
//
//+ (instancetype)resultWithDictionary:(NSDictionary *)dictionary analyseBlock:(PYResult *(^)(NSDictionary *))analyse {
//    PYResult *resuest = [[PYResult alloc] initWithDictionary:dictionary analyseBlock:analyse];
//    return resuest;
//}
//-(void)setAnalyseBlock:(AnalyseBlock)analyseBlock{
//    _analyseBlock = analyseBlock;
//    if(analyseBlock){
//        analyseBlock(self.dictionary);
//    }
//}

- (void)anasyleData:(NSDictionary *)data {
    if (data && data.count > 0) {
        self.message = data[@"message"];
        self.codeStyle = [data[@"code"] integerValue];
    }
    self.data = data[@"data"];
}
@end
